﻿using System.Net;
using Blazor.CoffeeTalesApi.Entity;
using Blazor.Model.Entity;

namespace Blazor.CoffeeTalesApi.Util;

public static class WhitelistKeyUtil
{
    public static IWhitelistKey FromString(string s)
    {
        if (s.StartsWith("ip:"))
        {
            var ip = IPAddress.Parse(s.AsSpan(3));
            return new IpKey(ip);
        }

        if (s.StartsWith("device_id:"))
        {
            return new DeviceIdKey(s.AsSpan(10));
        }
        
        throw new FormatException($"Invalid WhitelistKey format: {s}");
    }
}